package logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import util.Config;


/**
 * Simple Logger
 * @author Jakobus Böttcher
 *
 */
public class SimpleLogger {
	private static SimpleLogger logger = null;
	private static FileWriter fwr;
	private SimpleLogger(){
		try {
			fwr = new FileWriter(new File(Config.APP_DIR+"logs"+File.separator+"debug.log"),true);
			fwr.write("***************** NEW LOG ["+new java.util.Date().toString()+"]*****************"+Config.LINE_SEP);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void log(LogLevel level,String msg){
		
		if (logger == null){
			logger = new SimpleLogger();
		}
	
		try {
			logger.put(level, msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void put(LogLevel level,String msg) throws Exception {
		if (logger == null){
			throw new NullPointerException("Logger not initialised");
		}
		try {
			fwr.write(new String("["+level.name()+":"+new java.util.Date().toString()+"] "+msg+Config.LINE_SEP));
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fwr.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
